package com.iwish.server.app;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.concurrent.Executors;

import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.executor.ExecutorFilter;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import com.iwish.server.model.ServerServicHandler;

public class MinaServer {

	//接入端口
	private static final int PORT = 9123;

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		SocketAcceptor acceptor = new NioSocketAcceptor();
		acceptor.setReuseAddress(true);
		
		acceptor.getFilterChain().addLast("logger", new LoggingFilter());

		acceptor.getFilterChain().addLast(
				"codec",
				new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));

		acceptor.getFilterChain().addLast(
				"threadPool", 
				new ExecutorFilter(Executors.newCachedThreadPool()));
		
		acceptor.setHandler(new ServerServicHandler());

		acceptor.getSessionConfig().setReadBufferSize(2048);

		acceptor.bind(new InetSocketAddress(PORT));

		System.out.println("Listening on port " + PORT);

	}

}
